package com.xyc.gulimall.member.config;

import feign.RequestInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Configuration
public class GuliFeignConfig {

    /**
     * 用于远程调用携带cookie
     * 配置，无需注入
     * 该配置与session无关
     *
     * @return
     */
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            //老请求：request 新请求：requestTemplate
            //1、可以使用RequestContextHolder拿到刚进来的这个请求
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            //2、同步请求头数据，主要同步Cookie
            requestTemplate.header("cookie", request.getHeader("cookie"));

            System.out.println("RequestTemplate线程号：" + Thread.currentThread().getId());
        };
    }

}
